<?php
/**
 * Created by PhpStorm.
 * User: xeng
 * Date: 2018/1/3
 * Time: 19:23
 */
//权限管理 类
namespace Home\Controller;
use Think\Controller;
//use think\helper\hash\Md5;
use Think\Model;
class PowerController extends AuthController {
    //注册新员工
    public function register(){
        $this->CheckPower('9001');
        if(IS_POST){
            $new_employee['name']=htmlspecialchars($_POST['name']);
            if($_POST['pwd']==$_POST['pwd2']){
                $new_employee['pwd']=md5($_POST['pwd'].C('MD5_SALT'));
            }else{
                //$this->alert('两次输入密码不一致!','注册失败',3);
                $this->error("<span style='color:red;font-size:48px;'>失败</span>,两次输入密码不一致！");
                die;
            }
            $new_employee['role'] = htmlspecialchars($_POST['role']);
            $new_employee['email']  = htmlspecialchars($_POST['email']);
            $new_employee['iphone'] = htmlspecialchars($_POST['iphone']);
            if(!empty($_POST['power'])){
                foreach ($_POST['power'] as $value) //过滤 权限编码 只为数字
                {
                    if (!is_int($value)) // there are several ways to do this
                    {
                        $value=(int)$value;
                        $checkpower[]= $value;
                        if(substr($value, 1,1)==0)
                            $checkpower[]= (int)substr($value, 0,1);
                        else
                            $checkpower[]= (int)substr($value, 0,2);
                    }
                }
                
            }
            
            $new_employee['power'] = implode(',',array_unique($checkpower));
            $new_employee['power'] = $new_employee['power'].','; 
            if(!empty($new_employee)){
                D("admin")->add($new_employee);
                $this->success("add success ",'select',2);
            }else{
                $this->error("<h1 style='color: red;'>添加员工失败</h1> " ,U('register'),1);
            }
        }

        if(IS_GET){
            if($_GET['id']){                    // id 单个查询
                $where ="id=".$_GET['id'];
                $data = D('admin')->where($where)->find(); //dump($where);die;
                $data = $this->getRoleName($data);
                $data["powerName"] = $this->getPowerName($data);
                $this->assign("data",$data);
                $this->display('profile');
                $this->logUrl();
                die;
            }
            $this->display();
        }
        $this->logUrl();
    }

    //查询员工
    public function select(){
        if(IS_GET){
            if(!empty($_GET['key'])){ // 处理 搜索框 的数据
                if(intval($_GET['key'])!== 0){
                    $_GET['id']=intval($_GET['key']);
                }else if(is_string($_GET['key'])){
                    $_GET['name']=htmlspecialchars(ltrim($_GET['key']));
                }
            }
//            dump($_GET);

            if($_GET['id']){                    // id 单个查询
                $where ="id=".$_GET['id'];
                $data = D('admin')->where($where)->find(); //dump($where);die;
                $data = $this->getRoleName($data);
                $data["powerName"] = $this->getPowerName($data);
                //dump($data);
                $this->assign("data",$data);
                $this->display('profile');
                $this->logUrl();
                die;
            }
            if($_GET['name']){                  // name 单个查询
                $name ="name='".$_GET['name']."'";
                $data = D('admin')->where($name)->find();
                //处理数据
                $data = $this->getRoleName($data);
                $data["powerName"] = $this->getPowerName($data);
//              dump($data);
                $this->assign("data",$data);

                $this->display('profile');
                $this->logUrl();
                die;

            }
            if($_GET['role']){                  //role 查询一类
                $role ="role='".$_GET['role']."'";
                $lists = D('admin')->where($role)->select();
                $this->assign("lists",$lists);
//                dump($lists);
                $this->display('lists');

            }
            $this->logUrl();

        }
        if(IS_POST){//修改单个

        }else{//查询全部
            if(empty($_GET) && empty($_POST)){
                $lists=D('admin')->order('id desc')->select();
            }
        }

        $count = count($lists);
//        dump($lists);
        $this->assign('count',$count);
        $this->assign('lists',$lists);
        $this->display('lists');
    }

    //修改员工
    public function amend(){
        $this->CheckPower('9002');
        // var_dump($_GET['id']);
        if(IS_GET){ //展示
            if($_GET['id']){
                $where = "id=".$_GET['id']; //dump($where);die;
                $data = D("admin")->where($where)->find();
            }
            $data = $this->getRoleName($data);  //获取员工 角色名称
            $data['powerName'] = $this->getPowerName($data);  //权限
            // $userPower = explode(',',$data['power']);
            // var_dump($data['powerName'] );
            // $userSortPower =$this->userSortPower($userPower); //权限菜单 复选框 列表
            foreach ($data['powerName'] as $key => $value) {
                 $result[$key]=$value['id'];
            }
            
            $this->assign("data",$data);
            $this->assign("result",$result);
            // $this->assign("userSortPower",$userSortPower);
            $this->display();
        }

        if(IS_POST){ //修改
            //dump($_POST);
            $data['id']=$_POST['id'];
            $data['role']=$_POST['role'];

            if(!empty($_POST['power'])){
                foreach ($_POST['power'] as $value) //过滤 权限编码 只为数字
                {
                    if (!is_int($value)) // there are several ways to do this
                    {
                        $value=(int)$value;
                        $checkpower[]= $value;
                        if(substr($value, 1,1)==0)
                            $checkpower[]= (int)substr($value, 0,1);
                        else
                            $checkpower[]= (int)substr($value, 0,2);
                    }
                }
                
            }
            $new_employee['power'] = implode(',',array_unique($checkpower));
            $data['power'] = $new_employee['power'].','; 
            if(intval($data['id'])){
                $where="id=".$data['id'];  
                D("admin")->where($where)->save($data);
                $this->success("success ",'amend?id='.$data['id'],1);
            }else{
                $this->error("<h1>;(</h1>fail to amend " ,U('power/select'),1);
            }
        }
    }
    //黑名单
    public function blacklist(){
        if(IS_GET){ //展示
            if($_GET['id']){
                $where = "id=".$_GET['id']; //dump($where);die;
                $data = D("admin")->where($where)->find();
            }
            $this->assign("data",$data);
            $this->display();
        }
        if(IS_POST){ //修改
            if(intval($_POST['id'])){
                $where="id=".$_POST['id'];
                $blacklist  = I('post.blacklist_s');
                $str = explode(',',I('post.blacklist'));
                foreach ($str as $v){
                    if (strpos($blacklist, $v)!=false) {
                        $this->error('已经添加过了');
                    }
                }
                $data['blacklist'] =$blacklist .','.I('post.blacklist');
                D("admin")->where($where)->save($data);
                $this->success("success ",'select',1);
            }else{
                $this->error("<h1>;(</h1>fail to blacklist " ,U('power/select'),1);
            }
         }
    }
    /*
     * 删除员工
     * by get.id
     */
    public function del(){
        $id_str = $_GET['id'];
        $id_int = intval($_GET['id']);
        if($id_str!=null && strlen($id_str)==strlen($id_int) ){
            $where="id=".$id_str;
            if(M('admin')->where($where)->delete() ){
                $this->success("<h3 style='color: red;'>删除成功</h3>",'select');
            }else{
                $this->error('<span style="color:red">删除失败.</span>');
            } ;
        }else{
            $this->error('<span style="color:red">删除失败</span>');
        }
    }


    //遍历 所有权限 用户是否拥有
    public function userSortPower($userPower){
        $y=0;   // 1级菜单容器
        $r=0; //2 级菜单容器
        $power_all =M('power')->select();
        sort($power_all);               
        foreach ($power_all as &$v) {
            if($v["mgr"] == "0"){       //获取一级菜单
                $userSortPower[]=$v;
            }
            //dump($userSortPower);
            if($userSortPower[$y]){ //获取二级菜单
                for($i=0;$i<count($power_all);$i++){
                    if($userSortPower[$y]["id"]==$power_all[$i]["mgr"]){
                        $userSortPower[$y]["child"][$r]=$power_all[$i];
                        foreach ($userPower as $key){
                            if($key ==  $userSortPower[$y]["child"][$r]["id"]){
                                $userSortPower[$y]["child"][$r]['checked']="checked";
                            }else{
                                //$userSortPower[$y]["child"][$r]['checked']="";
                            }
                        }

                        $r++;
                    }
                }
            }
            $y++;
        }
//        dump($ignore_array);
        foreach ($userSortPower as $v){
            foreach ($v as $u){
                if($u['id']==0){
                    $u['child']='';
                }
            }
        }
        sort($userSortPower); //数组排序
        return $userSortPower;
    }


    //获取员工 角色名称
    public function getRoleName($data){
        if($data["role"]==1){
            $data['roleName']="管理员";
        }
        if($data['role']==2){
            $data['roleName']="研发人员";
        }
        if($data["role"]==3){
            $data["roleName"]="运营人员";
        }
        if($data['role']==4){
            $data['roleName']="客服人员";
        }
        return $data;
    }

    //获取 用户 权限菜单
    public function getPowerName($data){
        // var_dump($data);
        if($data['power']=='all'){
            $allPower =D("power")->select();
            // var_dump($allPower); 
            $h = 0; 
            foreach ($allPower as $key){
                if($key['id']>10){
                   $powerName[$h]['name']= $key['name'];
                   $powerName[$h]['id'] = $key['id'];  
                   $h++;
                }
            } 
        }else{
            $arr =explode(",",$data['power']);//切分到数组
            $allPower =D("power")->select();
            $h = 0;             //容器
            foreach ($arr as $vo){
                foreach ($allPower as $key){
                    if(($vo == $key['id']) && $key['id']>10){
                        $powerName[$h]['name']= $key['name'];
                        $powerName[$h]['id'] = $key['id'];
                        $h++;
                    }
                }
            } 
        }
        // var_dump($powerName);
        return $powerName;
    }

    //修改用户密码
    public function checkpwd(){
        $user=$_SESSION["ltq_game_admin_user"]['id'];
        $where ="id=".$user;
        $data = D('admin')->where($where)->find();  
        $this->assign('data',$data);
        $this->display();
    }
    public function checkmima(){
        if (IS_POST) { //修改 d7876e492b7507b6f725442c08dd73b8
            //登陆密码 分支
            if ($_POST['pwd']) {
                //验证权限
                //修改 登陆密码
                    $id = $_POST['id'];
                    $data['pwd'] = $_POST['pwd'];
                    $pwd=md5(I('post.pwd').C('MD5_SALT'));
                    $lists= $this->weblog("update ltq_admin set pwd='{$pwd}' where id={$id}");
                    if ($lists) {
                        $this->success("修改成功");
                    } else {
                        $this->error("修改失败");
                    }
            }
        }
    }

    //修改gameid
    public function gameid(){
        $this->CheckPower('9003');
        $old_gameid=$_POST['old_gameid'];  //原始游戏ID
        $new_gameid=$_POST['new_gameid'];  //新游戏ID
        if(IS_POST){
             $result= $this->game("
                        SELECT   *  FROM data_useraccount   WHERE  gameid = $old_gameid
                  ");
             if(!$result){
                    $this->error("暂无此用户(原始游戏ID)");
                 // $this->err404("暂无此用户(原始游戏ID)", "err");
             }else{
                 $result2= $this->game("
                        SELECT   *  FROM data_useraccount   WHERE  gameid = $new_gameid
                  ");
                 if($result2){
                    $this->error("您的新ID用户已经存在");
                    // $this->err404("您的新ID用户已经存在", "err");
                 }else{
                    $result3= $this->game("
                        UPDATE data_useraccount SET gameid=$new_gameid where gameid=$old_gameid;
                    ");
                    if($result3){
                         $this->success("用户修改成功");
                    }
                 }
             }
        }
        $this->display();
    }







}